package org.easy.web.api.socket;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.easy.tool.res.Result;
import org.easy.web.service.socket.SocketService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: wxl
 * @CreateTime: 2023/1/12 11:43
 */
@Api(value = "TCP SOCKET操作API",tags = "TCP SOCKET操作API")
@RestController
@RequestMapping("/api/socket/")
public class SocketController {

    private final SocketService socketService;

    public SocketController(SocketService socketService) {
        this.socketService = socketService;
    }

    @ApiOperation(value = "TCP客户端连接服务端",httpMethod = "POST")
    @PostMapping("connect")
    public Result<?> connect(){
        socketService.connectedToServer();
        return Result.success();
    }

    @ApiOperation(value = "TCP客户端向服务端发消息",httpMethod = "POST")
    @ApiImplicitParam(name = "msg",value = "消息",required = true)
    @PostMapping("send")
    public Result<?> send (@RequestParam("msg") String msg) {
        socketService.sendMsg(msg);
        return Result.success();
    }

    @ApiOperation(value = "TCP客户端断开服务端连接",httpMethod = "POST")
    @PostMapping("close")
    public Result<?> close(){
        socketService.close();
        return Result.success();
    }

}
